package zhoufu.sz.sanxiao_check.presents;

import android.text.TextUtils;
import javax.inject.Inject;
import rx.Subscription;
import rx.functions.Action1;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.event.Event;
import zhoufu.sz.sanxiao_check.event.RxBus;
import zhoufu.sz.sanxiao_check.models.BaseModel;
import zhoufu.sz.sanxiao_check.models.StreetInfoModel;
import zhoufu.sz.sanxiao_check.net.RequestHelper;
import zhoufu.sz.sanxiao_check.net.RequestRepository;
import zhoufu.sz.sanxiao_check.ui.contract.StreetDetailsContract;
import zhoufu.sz.sanxiao_check.utils.UIUtils;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/22
 */
public class StreetInfoPresenter implements StreetDetailsContract.Presenter
{

    private RequestRepository mRepository;

    private StreetDetailsContract.View view;

    private StreetInfoModel _model;

    public void setSID(String SID)
    {
        this.SID = SID;
    }

    private String SID;

    @Inject
    public StreetInfoPresenter(RequestRepository mRepository)
    {
        this.mRepository = mRepository;
        rxBusInit();
    }

    public void bindView(StreetDetailsContract.View view)
    {
        SID = view.SID();
        this.view = view;
    }

    @Override public void onStart()
    {
        mRepository
                .GetStreetByBuildingNo(view.RID(), SID,
                        view.Building_No(),
                        new RequestHelper.RequestCallBack<StreetInfoModel>()
                        {
                            @Override public void onPullStart()
                            {
                                view.pullStart();
                            }

                            @Override public void onPullFailud(Throwable throwable)
                            {
                                view.showToast(UIUtils.getString(R.string.toast_network_error));
                                view.pullFaild();
                            }

                            @Override public void onPullEnd()
                            {
                                view.pullEnd();
                            }

                            @Override public void onPullSuccessed(StreetInfoModel s)
                            {
                                if ( s.isSuccess() )
                                {
                                    if ( s.getData().size() > 0 )
                                    {
                                        view.setData(s.getData().get(0));
                                        _model = s.getData().get(0);
                                    } else
                                    {
                                        view.showToast("暂无数据");
                                    }
                                } else
                                {
                                    view.showToast(s.getErrMsg());
                                }
                            }
                        });
    }

    private void rxBusInit()
    {
        Subscription subscribe1 = RxBus.getDefault().toObserverable(Event.UpdateStreetInfo.class)
                .subscribe(new Action1<Event.UpdateStreetInfo>()
                {
                    @Override public void call(Event.UpdateStreetInfo updateStreetInfo)
                    {
                        view.setData(updateStreetInfo.getModel());
                    }
                });

        Subscription subscribe2 = RxBus.getDefault().toObserverable(Event.PlaceChangeRefresh.class)
                .subscribe(
                        new Action1<Event.PlaceChangeRefresh>()
                        {
                            @Override public void call(Event.PlaceChangeRefresh refreshStreetEvent)
                            {
                                view.event(refreshStreetEvent.getModel());
                            }
                        });
        mRepository.addSubscribe(subscribe1);
        mRepository.addSubscribe(subscribe2);
    }

    @Override public void onPause()
    {

    }

    @Override public void onDestroy()
    {
        mRepository.onDestory();
    }

    @Override public StreetInfoModel getStreetInfoModel()
    {
        return _model == null ? new StreetInfoModel() : _model;
    }

    @Override public void uploadPhoto(final String localPath)
    {
        mRepository.UpdatePhotoInfo(view.RID(), view.SID(),
                TextUtils.isEmpty(view.SID()) || "0".equals(view.SID()) ? 1 : 2,
                localPath, new RequestHelper.RequestCallBack<BaseModel>()
                {
                    @Override public void onPullStart()
                    {

                    }

                    @Override public void onPullFailud(Throwable throwable)
                    {
                        view.showToast("门头更新失败");
                        view.showDialog(localPath);
                    }

                    @Override public void onPullEnd()
                    {

                    }

                    @Override public void onPullSuccessed(BaseModel s)
                    {
                        if ( s.isSuccess() )
                        {
                            view.showToast("门头更新成功");
                        } else
                        {
                            view.showToast(s.getErrMsg());
                        }
                    }
                });
    }

    @Override public void updatePeopleNum(int IsLive, String Num)
    {
        mRepository.UpdateEmptyShopInfo(view.RID(), IsLive, Num,
                new RequestHelper.RequestCallBack<BaseModel>()
                {
                    @Override public void onPullStart()
                    {

                    }

                    @Override public void onPullFailud(Throwable throwable)
                    {
                        view.showToast("更新失败");
                    }

                    @Override public void onPullEnd()
                    {

                    }

                    @Override public void onPullSuccessed(BaseModel s)
                    {
                        if ( s.isSuccess() )
                        {
                            view.showToast("更新成功");
                        } else
                        {
                            view.showToast(s.getErrMsg());
                        }
                    }
                });
    }

    @Override public void StreeClose()
    {
        mRepository.StreeClose(SID, new RequestHelper.RequestCallBack<BaseModel>()
        {
            @Override public void onPullStart()
            {
                view.pullStart();
            }

            @Override public void onPullFailud(Throwable throwable)
            {
                view.pullFaild();
            }

            @Override public void onPullEnd()
            {
                view.pullEnd();
            }

            @Override public void onPullSuccessed(BaseModel s)
            {
                if ( s.isSuccess() )
                {
                    SID = "";
                    onStart();
                } else
                {
                    view.showToast(s.getErrMsg());
                }
            }
        });
    }
}
